<!DOCTYPE html>
<html>
	<script src="js/jquery-3.1.1.js"></script>
	<link href="static/assets/global/plugins/bootstrap/css/bootstrap.min.css" style="stylesheet" type="text/css" />
	<link href="static/assets/global/css/components.css" rel="stylesheet" type="text/css" />
	<link href="static/assets/admin/pages/css/login.css" rel="stylesheet" type="text/css" />
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body class="login">
		<div class="content">
			<h3 class="form-title">创建商品</h3>
			
			
			<div class="form-group">
				<label class="control-label">商品名</label>
				<div>
					<input class="form-control" type="text" name="title" id="title" />
				</div>
			</div>
			
			<div class="form-group">
				<label class="control-label">商品描述</label>
				<div>
					<input class="form-control" type="text" name="description" id="description" />
				</div>
			</div>
			
			<div class="form-group">
				<label class="control-label">价格</label>
				<div>
					<input class="form-control" type="text" name="price" id="price" />
				</div>
			</div>
			
			<div class="form-group">
				<label class="control-label">图片</label>
				<div>
					<input class="form-control" type="text" name="imgUrl" id="imgUrl" />
				</div>
			</div>
			
			<div class="form-group">
				<label class="control-label">库存</label>
				<div>
					<input class="form-control" type="text" name="stock" id="stock" />
				</div>
			</div>
			
			
			<div class="form-actions">
				<button class="btn blue" id="register">提交</button>
			</div>
		</div>
	</body>
	
	<script type="text/javascript">
		$(document).ready(function() {
						$("#register").on("click",function(){
							var title = $("#title").val();
							var description = $("#description").val();
							var price = $("#price").val();
							var stock = $("#stock").val();
							var imgUrl = $("#imgUrl").val();
							
							if(title == null ||title == ""){
								alert("商品名不能为空");
								return false;
							}
							if(description == null ||description == ""){
								alert("描述不能为空");
								return false;
							}
							if(price == null ||price == ""){
								alert("价格不能为空");
								return false;
							}
							if(stock == null ||stock == ""){
								alert("库存不能为空");
								return false;
							}
							if(imgUrl == null ||imgUrl == ""){
								alert("图片地址不能为空");
								return false;
							}
							
							$.ajax({
								type:"POST",
								contentType:"application/x-www-form-urlencoded",
								url:"/item/createItem",
								data:{
									"title":title,
									"description":description,
									"price":price,
									"stock":stock,
									"imgUrl":imgUrl
								},
								xhrFields:{withCredentials:true},
								success:function(data){
									console.log(data);
									if(data.status == "success"){
										alert("添加成功")
									}else{
										alert("添加失败，原因为"+data.data.errMsg)
									}
								},
								error:function(data){
									alert("添加失败，原因为"+data.data.responseText);
								}
							});
							return false;
						});
					});
	</script>
</html>
